package com.yequan.spring.bean.definition;

import com.yequan.spring.bean.factory.DefaultUserFactory;
import com.yequan.spring.bean.factory.UserFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 外部注册一个单例对象
 *
 * @author : Administrator
 * @date : 2020/4/6
 */
public class SingletonBeanRegistrationDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        UserFactory userFactory = new DefaultUserFactory();
        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        beanFactory.registerSingleton("userFactory1", userFactory);
        applicationContext.refresh();

        UserFactory userFactoryLookup = applicationContext.getBean("userFactory1", UserFactory.class);

        System.out.println("userFactoryLookup == userFactory ? " + (userFactoryLookup == userFactory));

        applicationContext.close();

    }

}
